<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/lib/layui-v2.7.6/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form" >
    <div class="layui-form-item">
        <label class="layui-form-label">编号</label>
        <div class="layui-input-block">
            <input type="number" name="serial_num" placeholder="" value="" lay-verify="required" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">花束名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" placeholder="" value="" lay-verify="required" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">花束图片</label>
        <div class="layui-input-block">
            <input type="hidden" name="img_id" id="productImg"/>
            <button type="button" class="layui-btn" id="productImgButton">上传图片</button>
            <div class="layui-upload-list">
                <img class="layui-upload-img" id="productImgImg">
                <p id="demoText"></p>
            </div>
        </div>

    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">价格</label>
        <div class="layui-input-block">
            <input type="number" name="price" placeholder="" lay-verify="required" value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">分类</label>
        <div class="layui-input-block">
            <select name="huashu_category_id" id="huashu_category_id" lay-verify="required">
                <option value=""></option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="checkbox" checked="" name="status" value="1" lay-skin="switch" lay-filter="switchTest" lay-text="启用|禁用">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="/static/lib/layui-v2.7.6/layui.js" charset="utf-8"></script>
<script src="/static/js/common.js" charset="utf-8"></script>
<script src="/static/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['form','upload','easyCropper'], function () {
        var form = layui.form,
            layer = layui.layer,
            easyCropper = layui.easyCropper,
            $ = layui.$;

        //渲染下拉
        $.ajax({
            url: "/category/index?searchParams="+JSON.stringify({"status":"1"}),
            type: 'get',
            contentType: "application/x-www-form-urlencoded; charset=UTF-8",
            async: false,
            success: function (ret) {
                $.each(ret.data,function (index,item){
                    $("#huashu_category_id").append(new Option(item.name,item.id))
                });
                layui.form.render('select')
            },
            error: function (xhr, textstatus, thrown) {
                layer.msg('操作失败');
            }
        });
            //创建一个图片裁剪上传组件
            var productImgCropper = easyCropper.render({
                elem: '#productImgButton'
                // ,defaultImg: '../static/images/noimg.jpg' // 默认图片 选填
                ,size: 20480    // 大小限制 默认1024k 选填
                ,saveW: 480    //保存宽度
                ,saveH: 600     //保存高度
                // ,mark:1/1   //选取比例
                ,fileType:["image/jpg", "image/png", "image/jpeg"]  //允许上传的图片类型
                ,area:'900px'  //弹窗宽度
                ,url: '/image/upload'  //图片上传接口返回和（layui 的upload 模块）返回的JOSN一样回的JOSN一样
                // ,choose: function(obj){}
                ,done: function(data){ //上传完毕回调
                    $("#productImg").val(data.id);
                    $("#productImgImg").attr('src',data.url);
                }
            });
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.ajax({
                url: "/huashu/add",
                type: 'post',
                contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                data:JSON.stringify({
                    "name": data.field.name,
                    "img_id": parseInt(data.field.img_id),
                    "price": parseInt(data.field.price),
                    "status": data.field.status == null ? 0 : parseInt(data.field.status),
                    "serial_num": parseInt(data.field.serial_num),
                    "huashu_category_id": parseInt(data.field.huashu_category_id),
                }),
                async: false,
                success: function (ret) {
                    var msg = ret.msg || "保存成功"
                    var index = layer.alert(JSON.stringify(data.field), {
                        title: msg
                    }, function () {
                        // 关闭弹出层
                        layer.close(index);
                        parent.layui.table.reload('currentTableId',{page:{curr:1}})
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });
                },
                error: function (xhr, textstatus, thrown) {
                    layer.msg('保存失败');
                }
            });
            return false;
        });
    });
</script>
</body>
</html>